package com.epalmpay.service.ys.memberRedActivityService.impl;

import com.epalmpay.dto.bonus.BonusSendLogListOutDTO;
import com.epalmpay.dto.bonus.BonusSendLogOutDTO;
import com.epalmpay.dto.yunliu.memberRedActivity.*;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.entity.YsMemberRedActivity;
import com.epalmpay.entity.YsMemberRedActivityPerfect;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.YunStreamEnum;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.mapper.YsMemberRedActivityMapper;
import com.epalmpay.mapper.YsMemberRedActivityPerfectMapper;
import com.epalmpay.service.ys.memberRedActivityService.IYsMemberRedActivityService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by GWN on 2018/1/9.
 */
@Service("memberRedActivityService")
public class YsMemberRedActivityServiceImpl implements IYsMemberRedActivityService {

    @Resource
    private YsMemberRedActivityMapper ysMemberRedActivityMapper;

    @Resource
    private YsMemberRedActivityPerfectMapper ysMemberRedActivityPerfectMapper;

    @Resource
    private TradeOfflineMapper tradeOfflineMapper;

    @Override
    public MemberRedActivityListOutDTO queryMemberRedActivityList(MemberRedActivityInDTO memberRedActivityInDTO, Page page) throws ApplicationException {
        MemberRedActivityListOutDTO memberRedActivityListOutDTO = new MemberRedActivityListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",memberRedActivityInDTO.getGroupId());
        param.put("memberRedActivityStatus",memberRedActivityInDTO.getRedActivityStatus());
        param.put("startTime",memberRedActivityInDTO.getStartTime());
        param.put("endTime",memberRedActivityInDTO.getEndTime());
//        param.put("redActivityType",memberRedActivityInDTO.getRedActivityType());



        int sumCount = ysMemberRedActivityMapper.queryMemberRedActivityCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return memberRedActivityListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<MemberRedActivityOutDTO> memberRedActivityOutDTOList = ysMemberRedActivityMapper.queryMemberRedActivityList(param);
        memberRedActivityListOutDTO.setMemberRedActivityOutDTOList(memberRedActivityOutDTOList);
        return memberRedActivityListOutDTO;
    }

    @Override
    public void saveMemberRedActivity(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        YsMemberRedActivity ysMemberRedActivity = new YsMemberRedActivity();
        ysMemberRedActivity.setGmtCreate(new Date());
        ysMemberRedActivity.setGmtModified(new Date());
        ysMemberRedActivity.setGroupId(memberRedActivityInDTO.getGroupId());
        ysMemberRedActivity.setActivityName(memberRedActivityInDTO.getActivityName());
        ysMemberRedActivity.setUrl(memberRedActivityInDTO.getUrl());
        ysMemberRedActivity.setStartTime(DateUtil.fomatDate(memberRedActivityInDTO.getJoinStart()));
        ysMemberRedActivity.setEndTime(DateUtil.fomatDate(memberRedActivityInDTO.getJoinEnd()));
        ysMemberRedActivity.setIsObdOpen(memberRedActivityInDTO.getIsObdOpen());
        ysMemberRedActivity.setRebateGetTime(memberRedActivityInDTO.getRebateGetTime());
        ysMemberRedActivity.setFacePeople(memberRedActivityInDTO.getFacePeople());
        if(memberRedActivityInDTO.getFacePeople()==1){
            ysMemberRedActivity.setGradeIds(memberRedActivityInDTO.getGradeIds());
        }
        ysMemberRedActivity.setIsPrimarySend(memberRedActivityInDTO.getIsPrimarySend());
        ysMemberRedActivity.setIsSeniorSend(memberRedActivityInDTO.getIsSeniorSend());
        ysMemberRedActivity.setOneRebateAmount(Long.parseLong(memberRedActivityInDTO.getOneRebateAmount().toString())*100);
        ysMemberRedActivity.setRebateTime(memberRedActivityInDTO.getRebateTime());
        ysMemberRedActivity.setIsWithdrawal(memberRedActivityInDTO.getIsWithdrawal());
        ysMemberRedActivity.setRedActivityType(memberRedActivityInDTO.getRedActivityType());
        ysMemberRedActivity.setRedActivityStatus(YunStreamEnum.MemberRedActivityStatus.doing.getType());

        ysMemberRedActivityMapper.insertSelective(ysMemberRedActivity);


    }

    @Override
    public MemberRedActivityOutDTO queryMemberRedActivityDetail(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityOutDTO memberRedActivityOutDTO = new MemberRedActivityOutDTO();

        memberRedActivityOutDTO= ysMemberRedActivityMapper.queryMemberRedActivityDetail(memberRedActivityInDTO);

        YsMemberRedActivityPerfect ysMemberRedActivityPerfect = ysMemberRedActivityPerfectMapper.queryData(memberRedActivityInDTO);
        memberRedActivityOutDTO.setAlreadyRebateAmount(ysMemberRedActivityPerfect.getAlreadyRebateAmount());
        return memberRedActivityOutDTO;
    }

    @Override
    public MemberRedActivityOutDTO editStatus(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityOutDTO memberRedActivityOutDTO = new MemberRedActivityOutDTO();

        List<YsMemberRedActivityPerfect> memberRedActivityPerfectList = ysMemberRedActivityPerfectMapper.queryList(memberRedActivityInDTO);
        for(YsMemberRedActivityPerfect memberRedActivityPerfect:memberRedActivityPerfectList){
            memberRedActivityPerfect.setRebateStatus(memberRedActivityInDTO.getRebateStatus());
            memberRedActivityPerfect.setGmtModified(new Date());
            ysMemberRedActivityPerfectMapper.updateByPrimaryKeySelective(memberRedActivityPerfect);
        }

        return memberRedActivityOutDTO;
    }

    @Override
    public MemberRedActivityPerfectListDTO queryPerfectDetailList(MemberRedActivityInDTO memberRedActivityInDTO, Page page) throws ApplicationException {
        MemberRedActivityPerfectListDTO memberRedActivityPerfectListDTO = new MemberRedActivityPerfectListDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",memberRedActivityInDTO.getGroupId());
        param.put("activityId",memberRedActivityInDTO.getId());
        param.put("rebateStatus",memberRedActivityInDTO.getRebateStatus());
        param.put("startTime",memberRedActivityInDTO.getStartTime());
        param.put("endTime",memberRedActivityInDTO.getEndTime());
        param.put("memberName",memberRedActivityInDTO.getMemberName());

        YsMemberRedActivity ysMemberRedActivity = ysMemberRedActivityMapper.selectByPrimaryKey(memberRedActivityInDTO.getId());
        memberRedActivityPerfectListDTO.setYsMemberRedActivity(ysMemberRedActivity);

        //查询列表
        int sumCount = ysMemberRedActivityPerfectMapper.queryMemberRedActivityPerfectCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return memberRedActivityPerfectListDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<MemberRedActivityPerfectOutDTO> memberRedActivityPerfectOutDTOList = ysMemberRedActivityPerfectMapper.queryMemberRedActivityPerfectList(param);
        memberRedActivityPerfectListDTO.setMemberRedActivityPerfectOutDTOList(memberRedActivityPerfectOutDTOList);
        return memberRedActivityPerfectListDTO;
    }

    @Override
    public MemberRedActivityPerfectOutDTO editRebateStatus(MemberRedActivityInDTO memberRedActivityInDTO) {
        MemberRedActivityPerfectOutDTO memberRedActivityPerfectOutDTO = new MemberRedActivityPerfectOutDTO();
        //查询这个会员全部还没返利的 返利 全部暂停
        List<YsMemberRedActivityPerfect> ysMemberRedActivityPerfectList = ysMemberRedActivityPerfectMapper.queryMemberAllRebateDate(memberRedActivityInDTO.getMemberId());

        for(YsMemberRedActivityPerfect ysMemberRedActivityPerfect :ysMemberRedActivityPerfectList){
            ysMemberRedActivityPerfect.setId(memberRedActivityInDTO.getId());
            ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.suspended.getType());
            ysMemberRedActivityPerfect.setGmtModified(new Date());
            ysMemberRedActivityPerfect.setIsRebate(1);
            ysMemberRedActivityPerfectMapper.updateByPrimaryKeySelective(ysMemberRedActivityPerfect);
        }

        return memberRedActivityPerfectOutDTO;
    }

    @Override
    public MemberRedActivityPerfectListDTO queryPerfectView(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityPerfectListDTO memberRedActivityPerfectListDTO = new MemberRedActivityPerfectListDTO();

        MemberRedActivityPerfectOutDTO memberRedActivityPerfectOutDTO = ysMemberRedActivityPerfectMapper.queryPerfectView(memberRedActivityInDTO);

        List<MemberRedActivityPerfectOutDTO> memberRedActivityPerfectOutDTOList = ysMemberRedActivityPerfectMapper.queryRebateData
                                        (memberRedActivityPerfectOutDTO.getMemberId(),memberRedActivityPerfectOutDTO.getActivityId());

        memberRedActivityPerfectListDTO.setMemberRedActivityPerfectOutDTOList(memberRedActivityPerfectOutDTOList);
        memberRedActivityPerfectListDTO.setMemberRedActivityPerfectOutDTO(memberRedActivityPerfectOutDTO);

        return memberRedActivityPerfectListDTO;
    }

    @Override
    public MemberRedActivityOutDTO hasU(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityOutDTO memberRedActivityOutDTO = new MemberRedActivityOutDTO();
        int count = ysMemberRedActivityMapper.hasU(memberRedActivityInDTO.getGroupId());
        if(count>=1){
            throw new ApplicationException(Rescode.FAIL,"活动未结束请先结束活动");
        }

        return memberRedActivityOutDTO;
    }

    @Override
    public MemberRedActivityPerfectOutDTO eralyRebate(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityPerfectOutDTO memberRedActivityPerfectOutDTO = new MemberRedActivityPerfectOutDTO();
        YsMemberRedActivityPerfect ys  = ysMemberRedActivityPerfectMapper.queryPerfectData(memberRedActivityInDTO.getId());
        //提前返利
        YsMemberRedActivityPerfect ysMemberRedActivityPerfect = new YsMemberRedActivityPerfect();
        ysMemberRedActivityPerfect.setId(memberRedActivityInDTO.getId());
        ysMemberRedActivityPerfect.setGmtModified(new Date());
        ysMemberRedActivityPerfect.setStatisDate(new Date());
        ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.earlyEnd.getType());
        ysMemberRedActivityPerfect.setIsRebate(1);
        ysMemberRedActivityPerfect.setAlreadyRebateAmount(Long.parseLong(memberRedActivityInDTO.getOneRebateAmount().toString()));
        int count=  ysMemberRedActivityPerfectMapper.updateByPrimaryKeySelective(ysMemberRedActivityPerfect);

        if(count==1){
            YsMemberRedActivity ysMemberRedActivity = new YsMemberRedActivity();
            ysMemberRedActivity.setId(ys.getActivityId());
            ysMemberRedActivity.setGmtModified(new Date());
            ysMemberRedActivity.setRebateAmount(Long.parseLong(memberRedActivityInDTO.getOneRebateAmount().toString()));
            ysMemberRedActivity.setRebateSum(1);
            ysMemberRedActivityMapper.updateYsMemberRedActivityDate(ysMemberRedActivity);

            TradeOffline tradeOffline = new TradeOffline();
            tradeOffline.setId(ys.getTradeId());
            tradeOffline.setGmtModified(new Date());
            tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());

            tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

        }else{
            throw new ApplicationException(Rescode.FAIL,"提前返利失败");
        }
        return memberRedActivityPerfectOutDTO;
    }

    @Override
    public MemberRedActivityPerfectOutDTO batchEralyRebate(MemberRedActivityInDTO memberRedActivityInDTO) throws ApplicationException {
        MemberRedActivityPerfectOutDTO memberRedActivityPerfectOutDTO = new MemberRedActivityPerfectOutDTO();
        List<YsMemberRedActivityPerfect> ysMemberRedActivityPerfectList = ysMemberRedActivityPerfectMapper.queryMemberAllRebateDate(memberRedActivityInDTO.getMemberId());
        for(YsMemberRedActivityPerfect ysMemberRedActivityPerfect :ysMemberRedActivityPerfectList){
            YsMemberRedActivityPerfect ys  = ysMemberRedActivityPerfectMapper.queryPerfectData(ysMemberRedActivityPerfect.getId());
            //提前返利
            ysMemberRedActivityPerfect.setId(memberRedActivityInDTO.getId());
            ysMemberRedActivityPerfect.setGmtModified(new Date());
            ysMemberRedActivityPerfect.setStatisDate(new Date());
            ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.earlyEnd.getType());
            ysMemberRedActivityPerfect.setIsRebate(1);
            ysMemberRedActivityPerfect.setAlreadyRebateAmount(Long.parseLong(memberRedActivityInDTO.getOneRebateAmount().toString()));
            int count=  ysMemberRedActivityPerfectMapper.updateByPrimaryKeySelective(ysMemberRedActivityPerfect);

            if(count==1){
                YsMemberRedActivity ysMemberRedActivity = new YsMemberRedActivity();
                ysMemberRedActivity.setId(ys.getActivityId());
                ysMemberRedActivity.setGmtModified(new Date());
                ysMemberRedActivity.setRebateAmount(Long.parseLong(memberRedActivityInDTO.getOneRebateAmount().toString()));
                ysMemberRedActivity.setRebateSum(1);
                ysMemberRedActivityMapper.updateYsMemberRedActivityDate(ysMemberRedActivity);

                TradeOffline tradeOffline = new TradeOffline();
                tradeOffline.setId(ys.getTradeId());
                tradeOffline.setGmtModified(new Date());
                tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());

                tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

            }else{
                throw new ApplicationException(Rescode.FAIL,"提前返利失败");
            }
        }

        return memberRedActivityPerfectOutDTO;
    }
}
